package com.financing.wap.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.wap.common.ResultMap;
import com.financing.wap.dao.IActivitieGiftDao;
import com.financing.wap.dao.IActivitiesGroupDao;
import com.financing.wap.dao.IUserWechatDao;
import com.financing.wap.dto.ActivitieGiftDTO;
import com.financing.wap.dto.ActivitiesGroupDTO;
import com.financing.wap.dto.FaUserWechatDTO;
import com.financing.wap.service.IActivitesGroupService;
import com.financing.wap.util.Constants;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.StringUtil;
import com.financing.wap.vo.ActivitiesGroupVO;


@Service("activitiesGroupService")
public class ActivitiesGroupServiceImpl extends BaseServiceImpl implements IActivitesGroupService{

	@Autowired
	private IActivitiesGroupDao activitiesGroupDao;
	
	@Autowired
	private IUserWechatDao userWechatDaoImpl;
	
	@Autowired
	private IActivitieGiftDao activitieGiftDaoImpl;

	@Override
	public String createActivitesGroup(ActivitiesGroupDTO agd) throws Exception {
		
		//微信为绑定的时候不允许建团
		if(!isBindingWechat(agd.getFpGroupHeadId())){
			return ResultMap.STATUS_CODE_ACTIVITIES_GROUP_NO_BINDING_WECHAT;
		}
		
		//查询目前所建立或者参加的的团，如果有进行中的团或者自己创建成功的团超过10个则不允许创建团
		int successGroup = 0;
		
		ActivitiesGroupDTO param =  new ActivitiesGroupDTO();
		param.setFpFaId(agd.getFpGroupHeadId());
		List<ActivitiesGroupDTO> listActivitesGroup = activitiesGroupDao.queryActivitiesGroup(param);
		for(ActivitiesGroupDTO agvo : listActivitesGroup){
			//状态成功直接统计 有状态在进行中团则不允许再建团
			if(Constants.ActivitiesGroupDTO.STATUS_SUCCESS_END.equals(agvo.getFpGroupStatus())&&agvo.getFpFaId().equals(agvo.getFpGroupHeadId())){
				successGroup++;
			}else if(Constants.ActivitiesGroupDTO.STATUS_CREATE.equals(agvo.getFpGroupStatus())){
				return ResultMap.STATUS_CODE_ACTIVITIES_GROUP_RUNNING;
			}
		}
		 if(successGroup >=11){
			 return ResultMap.STATUS_CODE_ACTIVITIES_GROUP_OVER_LIMIT;
		 }
		
		 //组ID从数据获取uuid
		 agd.setFpGroupId(activitiesGroupDao.getUUID());
		 activitiesGroupDao.addActivitiesGroup(agd);
		 
		return ResultMap.STATUS_CODE_SUCCESS;
	}

	@Override
	public String addActivitiesGroupMember(ActivitiesGroupDTO agd) throws Exception {
		
		//微信为绑定的时候不允许建团
		if(!isBindingWechat(agd.getFpFaId())){
			return ResultMap.STATUS_CODE_ACTIVITIES_GROUP_NO_BINDING_WECHAT;
		}
		
		ActivitiesGroupDTO param =  new ActivitiesGroupDTO();
		
		//判断是否已经加过团队
		param.setFpFaId(agd.getFpFaId());
		List<ActivitiesGroupDTO> inGroup = activitiesGroupDao.queryActivitiesGroup(param);
		if(!(inGroup == null || inGroup.isEmpty())){
			return ResultMap.STATUS_CODE_ACTIVITIES_GROUP_IN_GROUP;
		}
		
		//置空faId 为了后面查询准备条件
		param.setFpFaId(null);
		
		param.setFpGroupId(agd.getFpGroupId());
		List<ActivitiesGroupDTO> listActivitiesGroup = activitiesGroupDao.queryActivitiesGroup(param);
		
		//判断团队是否存在
		if(listActivitiesGroup == null || listActivitiesGroup.isEmpty()){
			return ResultMap.STATUS_CODE_ACTIVITIES_GROUP_NOT_EXIS;
		}
		
		ActivitiesGroupDTO ag = listActivitiesGroup.get(0);
		
		//查询当前团队状态
		param.setFpGroupHeadId(ag.getFpGroupHeadId());
		List<ActivitiesGroupVO> listVo = activitiesGroupDao.queryActivitiesGroupSuccess(param);
		
		
		if(!(listVo == null) && !(listVo.isEmpty())){
			//团结束或者已经满员不允许加入
			if(Constants.ActivitiesGroupDTO.STATUS_SUCCESS_END.equals(ag.getFpGroupStatus())){
				return ResultMap.STATUS_CODE_ACTIVIITES_GROUP_FULL;
			}else if(listVo.get(0).getTotalSuccessMember() >= ag.getFpGroupTotalMember()){
				return ResultMap.STATUS_CODE_ACTIVIITES_GROUP_FULL;
			}else if(StringUtil.rollDay(ag.getFpGroupCreateTime(),2).before(new Date())){
				return ResultMap.STATUS_CODE_ACTIVITIES_GROUP_TIME_OUT;
			}else if(Constants.ActivitiesGroupDTO.STATUS_FAIL_END.equals(ag.getFpGroupStatus())){
				return ResultMap.STATUS_CODE_ACTIVITIES_GROUP_FAIL_END;
			}
		}
		
		
		agd.setFpGroupName(ag.getFpGroupName());
		agd.setFpGroupCreateTime(ag.getFpGroupCreateTime());
		agd.setFpGroupTotalMember(ag.getFpGroupTotalMember());
		agd.setFpGroupHeadId(ag.getFpGroupHeadId());
		agd.setFpGroupTotalAmount(ag.getFpGroupTotalAmount());
		agd.setFpGroupSendAmount(new BigDecimal(0));
		agd.setFpGroupStatus(ag.getFpGroupStatus());
		activitiesGroupDao.addActivitiesGroup(agd);
		
		return ResultMap.STATUS_CODE_SUCCESS;
	}

	@Override
	public List<ActivitiesGroupDTO> queryFaOwnGroup(ActivitiesGroupDTO agd) throws Exception {
//		agd.setFpFaId(agd.getFpGroupHeadId());
		return activitiesGroupDao.queryActivitiesGroup(agd);
	}

	@Override
	public ActivitiesGroupVO queryShareActivitiesGroup(ActivitiesGroupDTO agd) throws Exception {
		
		List<ActivitiesGroupVO> list = activitiesGroupDao.queryShareActivitiesGroup(agd);
		if(list==null|| list.isEmpty()){
			return null;
		}
		return list.get(0);
	}

	/**
	 * 判断是否绑定微信
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	private boolean isBindingWechat(String userId) throws Exception{
		FaUserWechatDTO fwdto = new FaUserWechatDTO();
		fwdto.setUserId(userId);
		fwdto = userWechatDaoImpl.queryUserWechatInfo(fwdto);
		
		if(null == fwdto || null == fwdto.getId()){
			return false;
		}
		return true;
	}

	@Override
	public List<ActivitiesGroupVO> queryActivitiesGroupMemberInfo(ActivitiesGroupDTO agd) throws Exception {
		return activitiesGroupDao.queryActivitiesGroupMemberInfo(agd);
	}

	@Override
	public List<ActivitiesGroupVO> queryActivitiesGroupSendAmount(ActivitiesGroupDTO agd) throws Exception {
		
		List<ActivitiesGroupVO> list = activitiesGroupDao.queyActivitiesGroupGift(agd);
		
//		List<ActivitiesGroupVO> listRes = new ArrayList<ActivitiesGroupVO>();
		
		//去除打开的红包
//		for(ActivitiesGroupVO agvo : list){
//			if("01".equals(agvo.getGiftStatus())){
//				listRes.add(agvo);
//			}
//		}
		
		return list;
	}

	@Override
	public BigDecimal queryOneUserNotGetGiftAmount(ActivitiesGroupDTO agd) throws Exception {
		return activitiesGroupDao.queryOneUserNotGetGiftAmount(agd);
	}

	@Override
	public String openGift(ActivitiesGroupDTO agd) throws Exception {
		
		List<ActivitiesGroupDTO> list = activitiesGroupDao.queryActivitiesGroup(agd);
		if(null != list && list.size() != 1){
			return ResultMap.STATUS_CODE_ACTIVITIES_GROUP_RED_ERROR;
		}
		
		agd = list.get(0);
		
		if(StringUtil.isNullOrEmptyStr(agd.getFpGroupSendAmountStatus())){
			return ResultMap.STATUS_CODE_ACTIVITIES_GROUP_NO_RED;
		}
		//红包已拆
		if("2".equals(agd.getFpGroupSendAmountStatus())){
			return ResultMap.STATUS_CODE_ACTIVITIES_GROUP_RED_STATUS_ERROR;
		}
		
		agd.setFpGroupSendAmountStatus("2");
		DTOdateSetUtil.setUpdateInfo(agd);
		activitiesGroupDao.updateActivitiesGroup(agd);
		
		ActivitieGiftDTO agf = new ActivitieGiftDTO();
		agf.setFaUserId(agd.getFpFaId());
		agf.setActiviesCode("cd500");
		agf.setaAmount(agd.getFpGroupSendAmount());
		agf.setaType("01");
		agf.setaStatus("01");
		DTOdateSetUtil.setCreateInfo(agf);
		DTOdateSetUtil.setUpdateInfo(agf);
		activitieGiftDaoImpl.insertActivitieGift(agf);
		return ResultMap.STATUS_CODE_SUCCESS;
	}

	@Override
	public BigDecimal getGroupAmount(ActivitiesGroupDTO agd) throws Exception {
		return activitiesGroupDao.queryOneUserAllGiftAmount(agd);
	}

	@Override
	public int updateActivitiesGroupName(ActivitiesGroupDTO agd) throws Exception {
		return activitiesGroupDao.updateActivitiesGroup(agd);
	}
}
